﻿Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Partial Class FrontEnd_Control_Users_Login
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hplRegist.NavigateUrl = MainDomain & "dang-ky.html"
            'Check if the browser support cookies     
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            If Request.Browser.Cookies Then
                If Request.Cookies("FRONTENDLOGIN") IsNot Nothing Then
                    txtUserName.Text = Request.Cookies("FRONTENDLOGIN")("USERNAME").ToString()

                End If
            End If
        End If
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim strEncryptPass As String = Encrypt(txtPassword.Text)
        Using data As New VSEntities
            'type = 5 nguoi dung ben ngoai
            Dim p As User = (From q In data.Users Where q.UserName = txtUserName.Text.Trim And q.Password = strEncryptPass And q.IsUser = 5).FirstOrDefault
            If (Not p Is Nothing) Then
                Session("UserName") = p.UserName
                Session("UserId") = p.UserId
                Session("UserType") = p.IsUser
                Session("UserVip") = 0
                Session("UserCurHuyenId") = p.HuyenId
                Dim intUserId As Integer = p.UserId
                Session("IsUser") = p.IsUser
                p.LastLogin = Now

                data.SaveChanges()

                Dim retURL As String = IIf(Request("retURL") Is Nothing, MainDomain & "index.aspx", Request("retURL"))
                Response.Redirect(retURL)
            Else
                Excute_Javascript("Alertbox('Tên đăng nhập hoặc mật khẩu chưa chính xác, hoặc tài khoản chưa được kích hoạt.');", Me.Page, True)
            End If
        End Using
    End Sub

    Protected Sub hplRemember_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplRemember.Init
        hplRemember.NavigateUrl = MainDomain & "quen-mat-khau.html"
    End Sub
End Class
